package com.zy.gpdemo.dao;

import java.sql.Timestamp;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.zy.gpdemo.domain.PrintTask;

@Repository
@Mapper
public interface HsqldbDao {
	@Select("select * from task where status=#{status} and warehouseCode=#{warehouseCode} and stockCode=#{stockCode}")
	List<PrintTask> findTask(@Param("status") int status, @Param("warehouseCode") String warehouseCode,@Param("stockCode") String stockCode);

	@Update("update task set status=#{status}, lastTime=#{lastTime} where id=#{id}")
	int updateTask(@Param("status") int status,@Param("lastTime") Timestamp lastTime, @Param("id") String id);

	/**
	 * 插入任务，返回主键
	 * @param task
	 * @return	1或者0
	 */
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	@Insert("insert ignore into task(messageId,warehouseCode,appRoot,reportName,stockCode,reportParams,printNums,status,lastTime) values(#{task.messageId},#{task.warehouseCode},#{task.appRoot},#{task.reportName},#{task.stockCode},#{task.reportParams},#{task.printNums},#{task.status},#{task.lastTime})")
	int insertTask(@Param("task") PrintTask task);
	
	@Select("select count(*) from task where status=#{status} and warehouseCode=#{warehouseCode} and stockCode=#{stockCode} and datediff(lastTime,#{lastTime})=0")
	int findTaskNum(@Param("status") int status, @Param("warehouseCode") String warehouseCode,@Param("stockCode") String stockCode,@Param("lastTime") String lastTime);
	
	@Select("select * from task where status=-1 and warehouseCode=#{warehouseCode} and stockCode=#{stockCode} order by lastTime desc limit #{start},#{num}")
	List<PrintTask> findHistory(@Param("warehouseCode") String warehouseCode,@Param("stockCode") String stockCode,@Param("start") int start,@Param("num") int num);
	
	@Delete("delete from task where lastTime < #{lastTime}")
	int deleteHistory(@Param("lastTime") String lastTime);
}
